﻿using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x020004BB RID: 1211
	public class GuideInput : MonoBehaviour, IPointerDownHandler, IEventSystemHandler
	{
		// Token: 0x1700036E RID: 878
		// (set) Token: 0x06001EA6 RID: 7846 RVA: 0x000C00BC File Offset: 0x000BE2BC
		public GuideObject guideObject
		{
			set
			{
				if (this.SetGuideObject(value))
				{
					if (this.hashSelectObject.Count != 0)
					{
						this.SetInputText();
					}
					else
					{
						this.visible = false;
					}
				}
			}
		}

		// Token: 0x1700036F RID: 879
		// (get) Token: 0x06001EA7 RID: 7847 RVA: 0x000C00EC File Offset: 0x000BE2EC
		// (set) Token: 0x06001EA8 RID: 7848 RVA: 0x000C00F4 File Offset: 0x000BE2F4
		public bool outsideVisible
		{
			get
			{
				return this.m_OutsideVisible;
			}
			set
			{
				if (Utility.SetStruct<bool>(ref this.m_OutsideVisible, value))
				{
					this.SetVisible();
				}
			}
		}

		// Token: 0x17000370 RID: 880
		// (get) Token: 0x06001EA9 RID: 7849 RVA: 0x000C0110 File Offset: 0x000BE310
		// (set) Token: 0x06001EAA RID: 7850 RVA: 0x000C0118 File Offset: 0x000BE318
		public bool visible
		{
			get
			{
				return this.m_Visible;
			}
			set
			{
				if (Utility.SetStruct<bool>(ref this.m_Visible, value))
				{
					this.SetVisible();
				}
			}
		}

		// Token: 0x17000371 RID: 881
		// (get) Token: 0x06001EAB RID: 7851 RVA: 0x000C0134 File Offset: 0x000BE334
		// (set) Token: 0x06001EAC RID: 7852 RVA: 0x000C013C File Offset: 0x000BE33C
		public int selectIndex { get; set; }

		// Token: 0x17000372 RID: 882
		// (get) Token: 0x06001EAD RID: 7853 RVA: 0x000C0148 File Offset: 0x000BE348
		// (set) Token: 0x06001EAE RID: 7854 RVA: 0x000C0150 File Offset: 0x000BE350
		private InputField[] arrayInput { get; set; }

		// Token: 0x06001EAF RID: 7855 RVA: 0x000C015C File Offset: 0x000BE35C
		public void Stop()
		{
			this.hashSelectObject.Clear();
			this.visible = false;
		}

		// Token: 0x06001EB0 RID: 7856 RVA: 0x000C0170 File Offset: 0x000BE370
		public void OnEndEditPos(int _target)
		{
			if (this.hashSelectObject.Count == 0)
			{
				return;
			}
			float num = this.InputToFloat(this.inputPos[_target]);
			List<GuideCommand.EqualsInfo> list = new List<GuideCommand.EqualsInfo>();
			foreach (GuideObject guideObject in this.hashSelectObject)
			{
				if (guideObject.enablePos)
				{
					Vector3 pos = guideObject.changeAmount.pos;
					if (pos[_target] != num)
					{
						pos[_target] = num;
						Vector3 pos2 = guideObject.changeAmount.pos;
						guideObject.changeAmount.pos = pos;
						list.Add(new GuideCommand.EqualsInfo
						{
							dicKey = guideObject.dicKey,
							oldValue = pos2,
							newValue = pos
						});
					}
				}
			}
			if (!list.IsNullOrEmpty<GuideCommand.EqualsInfo>())
			{
				Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.MoveEqualsCommand(list.ToArray()));
			}
			this.SetInputTextPos();
		}

		// Token: 0x06001EB1 RID: 7857 RVA: 0x000C0294 File Offset: 0x000BE494
		public void OnEndEditRot(int _target)
		{
			if (this.hashSelectObject.Count == 0)
			{
				return;
			}
			float num = this.InputToFloat(this.inputRot[_target]) % 360f;
			List<GuideCommand.EqualsInfo> list = new List<GuideCommand.EqualsInfo>();
			foreach (GuideObject guideObject in this.hashSelectObject)
			{
				if (guideObject.enableRot)
				{
					Vector3 rot = guideObject.changeAmount.rot;
					if (rot[_target] != num)
					{
						rot[_target] = num;
						Vector3 rot2 = guideObject.changeAmount.rot;
						guideObject.changeAmount.rot = rot;
						list.Add(new GuideCommand.EqualsInfo
						{
							dicKey = guideObject.dicKey,
							oldValue = rot2,
							newValue = rot
						});
					}
				}
			}
			if (!list.IsNullOrEmpty<GuideCommand.EqualsInfo>())
			{
				Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.RotationEqualsCommand(list.ToArray()));
			}
			this.SetInputTextRot();
		}

		// Token: 0x06001EB2 RID: 7858 RVA: 0x000C03BC File Offset: 0x000BE5BC
		public void OnEndEditScale(int _target)
		{
			if (this.hashSelectObject.Count == 0)
			{
				return;
			}
			float num = Mathf.Max(this.InputToFloat(this.inputScale[_target]), 0.01f);
			List<GuideCommand.EqualsInfo> list = new List<GuideCommand.EqualsInfo>();
			foreach (GuideObject guideObject in this.hashSelectObject)
			{
				if (guideObject.enableScale)
				{
					Vector3 scale = guideObject.changeAmount.scale;
					if (scale[_target] != num)
					{
						scale[_target] = num;
						Vector3 scale2 = guideObject.changeAmount.scale;
						guideObject.changeAmount.scale = scale;
						list.Add(new GuideCommand.EqualsInfo
						{
							dicKey = guideObject.dicKey,
							oldValue = scale2,
							newValue = scale
						});
					}
				}
			}
			if (!list.IsNullOrEmpty<GuideCommand.EqualsInfo>())
			{
				Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.ScaleEqualsCommand(list.ToArray()));
			}
			this.SetInputTextScale(Vector3.zero);
		}

		// Token: 0x06001EB3 RID: 7859 RVA: 0x000C04F0 File Offset: 0x000BE6F0
		public void SetInputText()
		{
			this.visible = true;
			bool flag = this.hashSelectObject.Any((GuideObject v) => !v.enablePos);
			bool flag2 = this.hashSelectObject.Any((GuideObject v) => !v.enableRot);
			bool flag3 = this.hashSelectObject.Any((GuideObject v) => !v.enableScale);
			this.SetInputTextPos();
			for (int i = 0; i < 3; i++)
			{
				this.inputPos[i].interactable = !flag;
			}
			this.SetInputTextRot();
			for (int j = 0; j < 3; j++)
			{
				this.inputRot[j].interactable = !flag2;
			}
			this.SetInputTextScale(Vector3.zero);
			for (int k = 0; k < 3; k++)
			{
				this.inputScale[k].interactable = !flag3;
			}
		}

		// Token: 0x06001EB4 RID: 7860 RVA: 0x000C060C File Offset: 0x000BE80C
		private bool SetGuideObject(GuideObject _object)
		{
			if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
			{
				if (this.hashSelectObject.Contains(_object))
				{
					return false;
				}
				this.AddObject(_object);
			}
			else
			{
				foreach (GuideObject guideObject in this.hashSelectObject)
				{
					ChangeAmount changeAmount = guideObject.changeAmount;
					ChangeAmount changeAmount2 = changeAmount;
					changeAmount2.onChangePos = (Action)Delegate.Remove(changeAmount2.onChangePos, new Action(this.SetInputTextPos));
					ChangeAmount changeAmount3 = changeAmount;
					changeAmount3.onChangeRot = (Action)Delegate.Remove(changeAmount3.onChangeRot, new Action(this.SetInputTextRot));
					ChangeAmount changeAmount4 = changeAmount;
					changeAmount4.onChangeScale = (Action<Vector3>)Delegate.Remove(changeAmount4.onChangeScale, new Action<Vector3>(this.SetInputTextScale));
				}
				this.hashSelectObject.Clear();
				this.AddObject(_object);
			}
			return true;
		}

		// Token: 0x06001EB5 RID: 7861 RVA: 0x000C0720 File Offset: 0x000BE920
		private void AddObject(GuideObject _object)
		{
			if (_object == null)
			{
				return;
			}
			ChangeAmount changeAmount = _object.changeAmount;
			ChangeAmount changeAmount2 = changeAmount;
			changeAmount2.onChangePos = (Action)Delegate.Combine(changeAmount2.onChangePos, new Action(this.SetInputTextPos));
			ChangeAmount changeAmount3 = changeAmount;
			changeAmount3.onChangeRot = (Action)Delegate.Combine(changeAmount3.onChangeRot, new Action(this.SetInputTextRot));
			ChangeAmount changeAmount4 = changeAmount;
			changeAmount4.onChangeScale = (Action<Vector3>)Delegate.Combine(changeAmount4.onChangeScale, new Action<Vector3>(this.SetInputTextScale));
			this.hashSelectObject.Add(_object);
		}

		// Token: 0x06001EB6 RID: 7862 RVA: 0x000C07B4 File Offset: 0x000BE9B4
		private void SetInputTextPos()
		{
			GuideObject guideObject = this.hashSelectObject.ElementAtOrDefault(0);
			Vector3 vector = (!guideObject) ? Vector3.zero : guideObject.changeAmount.pos;
			bool[] array = new bool[]
			{
				true,
				true,
				true
			};
			foreach (GuideObject guideObject2 in this.hashSelectObject)
			{
				Vector3 pos = guideObject2.changeAmount.pos;
				for (int i = 0; i < 3; i++)
				{
					array[i] = (vector[i] == pos[i]);
				}
			}
			for (int j = 0; j < 3; j++)
			{
				this.inputPos[j].text = ((!array[j]) ? "-" : vector[j].ToString("0.000"));
			}
		}

		// Token: 0x06001EB7 RID: 7863 RVA: 0x000C08D4 File Offset: 0x000BEAD4
		private void SetInputTextRot()
		{
			GuideObject guideObject = this.hashSelectObject.ElementAtOrDefault(0);
			Vector3 vector = (!guideObject) ? Vector3.zero : guideObject.changeAmount.rot;
			bool[] array = new bool[]
			{
				true,
				true,
				true
			};
			foreach (GuideObject guideObject2 in this.hashSelectObject)
			{
				Vector3 rot = guideObject2.changeAmount.rot;
				for (int i = 0; i < 3; i++)
				{
					array[i] = (vector[i] == rot[i]);
				}
			}
			for (int j = 0; j < 3; j++)
			{
				this.inputRot[j].text = ((!array[j]) ? "-" : vector[j].ToString("0.000"));
			}
		}

		// Token: 0x06001EB8 RID: 7864 RVA: 0x000C09F4 File Offset: 0x000BEBF4
		private void SetInputTextScale(Vector3 _value)
		{
			GuideObject guideObject = this.hashSelectObject.ElementAtOrDefault(0);
			Vector3 vector = (!guideObject) ? Vector3.zero : guideObject.changeAmount.scale;
			bool[] array = new bool[]
			{
				true,
				true,
				true
			};
			foreach (GuideObject guideObject2 in this.hashSelectObject)
			{
				Vector3 scale = guideObject2.changeAmount.scale;
				for (int i = 0; i < 3; i++)
				{
					array[i] = (vector[i] == scale[i]);
				}
			}
			for (int j = 0; j < 3; j++)
			{
				this.inputScale[j].text = ((!array[j]) ? "-" : vector[j].ToString("0.000"));
			}
		}

		// Token: 0x06001EB9 RID: 7865 RVA: 0x000C0B14 File Offset: 0x000BED14
		private float InputToFloat(InputField _input)
		{
			float num = 0f;
			return (!float.TryParse(_input.text, out num)) ? 0f : num;
		}

		// Token: 0x06001EBA RID: 7866 RVA: 0x000C0B44 File Offset: 0x000BED44
		private bool Vector3Equals(ref Vector3 _a, ref Vector3 _b)
		{
			return _a.x == _b.x && _a.y == _b.y && _a.z == _b.z;
		}

		// Token: 0x06001EBB RID: 7867 RVA: 0x000C0B7C File Offset: 0x000BED7C
		private void SetVisible()
		{
			bool flag = this.m_OutsideVisible & this.m_Visible;
			if (this.canvasParent)
			{
				this.canvasParent.enabled = flag;
			}
			else
			{
				base.gameObject.SetActive(flag);
			}
		}

		// Token: 0x06001EBC RID: 7868 RVA: 0x000C0BC4 File Offset: 0x000BEDC4
		public void OnPointerDown(PointerEventData eventData)
		{
			SortCanvas.select = this.canvasParent;
		}

		// Token: 0x06001EBD RID: 7869 RVA: 0x000C0BD4 File Offset: 0x000BEDD4
		private void ChangeTarget()
		{
			bool flag = Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift) | Input.GetKeyDown(KeyCode.LeftShift) | Input.GetKeyDown(KeyCode.RightShift);
			if (flag)
			{
				this.selectIndex--;
				if (this.selectIndex < 0)
				{
					this.selectIndex = this.arrayInput.Length - 1;
				}
			}
			else
			{
				this.selectIndex = (this.selectIndex + 1) % this.arrayInput.Length;
			}
			if (this.arrayInput[this.selectIndex])
			{
				this.arrayInput[this.selectIndex].Select();
			}
		}

		// Token: 0x06001EBE RID: 7870 RVA: 0x000C0C84 File Offset: 0x000BEE84
		private void Awake()
		{
			this.visible = false;
			List<InputField> list = new List<InputField>();
			list.AddRange(this.inputPos);
			list.AddRange(this.inputRot);
			list.AddRange(this.inputScale);
			this.arrayInput = list.ToArray();
			this.selectIndex = -1;
		}

		// Token: 0x06001EBF RID: 7871 RVA: 0x000C0CD8 File Offset: 0x000BEED8
		private void Start()
		{
			(from _ in this.UpdateAsObservable()
			where this.selectIndex != -1
			where Input.GetKeyDown(KeyCode.Tab)
			select _).Subscribe(delegate(Unit _)
			{
				this.ChangeTarget();
			});
		}

		// Token: 0x040020B3 RID: 8371
		[SerializeField]
		protected InputField[] inputPos;

		// Token: 0x040020B4 RID: 8372
		[SerializeField]
		protected InputField[] inputRot;

		// Token: 0x040020B5 RID: 8373
		[SerializeField]
		protected InputField[] inputScale;

		// Token: 0x040020B6 RID: 8374
		[SerializeField]
		private Canvas canvasParent;

		// Token: 0x040020B7 RID: 8375
		private HashSet<GuideObject> hashSelectObject = new HashSet<GuideObject>();

		// Token: 0x040020B8 RID: 8376
		private bool m_OutsideVisible = true;

		// Token: 0x040020B9 RID: 8377
		private bool m_Visible = true;
	}
}
